{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 未 删 除 停 用 词 的 前 10 个 高 频 词 ：[('，', 59), ('的', 32), ('。', 22), ('、', 17), ('冬奥会', 16), ('\\n', 16), ('北京', 15), ('中国', 13), ('会徽', 12), ('年', 11)]\n",
      " 删 除 停 用 词 的 前 10 个 高 频 词 ：[('冬奥会', 16), ('北京', 15), ('中国', 13), ('会徽', 12), ('年', 11), ('冬', 8), ('2022', 7), ('运动员', 7), ('冰雪', 6), ('中心', 5)]\n"
     ]
    }
   ],
   "source": [
    "import jieba\n",
    "def TF_word(words, topk):     #统计词频\n",
    "    data={}                 #初始化空列表\n",
    "    for k in words:        #遍历词列表\n",
    "        if k in data:\n",
    "            data[k] += 1\n",
    "        else:\n",
    "            data[k] = 1\n",
    "    #将字典中的值按降序排列\n",
    "    data=sorted(data.items(),key=lambda x:x[1],reverse=True)\n",
    "    return data[:topk]\n",
    "file_content=open('C:/Users/Administrator/Desktop/data/dongaohui.txt',encoding='utf-8').read()\n",
    "split_words=jieba.lcut(file_content)\n",
    "print(\" 未 删 除 停 用 词 的 前 {0} 个 高 频 词 ：{1}\".format(10,TF_word(split_words,10)))\n",
    "#读取停用词\n",
    "stop_words=[line.strip() for line in open('C:/Users/Administrator/Desktop/data/stops_list.txt','r',encoding='utf-8').readlines()]\n",
    "stop_words.append('\\n')\n",
    "split_words2=[x for x in split_words if x not in stop_words]\n",
    "print(\" 删 除 停 用 词 的 前 {0} 个 高 频 词 ：{1}\".format(10,TF_word(split_words2,10)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
